package com.qdxwx.web.controller.distribution;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.enums.CourseType;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.member.MemberType;
import com.qdxwx.models.category.Category;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/*分销广场*/
@Controller
@RequestMapping("/distribution")
public class DistriButionController  extends BaseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private WebsiteProfileService websiteProfileService;
    @Autowired
    private MemberTypeService memberTypeService;
    @Autowired
    private ChannelService channelService;

    /**
     * 分销课程列表展示,搜索课程
     */
    @RequestMapping("/list.html")
    public String showCourseList(HttpServletRequest request, ModelMap model) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"), 2);
        int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
        int categoryParentId = 0;
        if (categoryId > 0) {
            Category category = categoryService.get(categoryId);
            if (category == null) {
                categoryId = 0;
            } else {
                channelId = category.getChannelId();
                categoryParentId = category.getParentId();
                model.put("category", category);
            }
        }
        model.put("categoryParentId", categoryParentId);//父级id

        Channel channel = channelService.get(channelId);
        if (channel == null) {
            return toErrorView(request, "找不到对应的频道！");
        }
        //禁用的频道
        if (channel.getIsDisabled() == 1) {
            return toErrorView(request, "当前频道已被禁用！");
        }
        //判断是否属于模块
        if (channel.getModuleId() != ModuleType.COURSE.ordinal()) {
            return toErrorView(request, "当前频道归属错误！");
        }
        model.put("channelId", channel.getChannelId());
        model.put("channel", channel);
        // 查询所有1级专业
        List<Category> categoryRoots = categoryService.getRoots(channelId);
        model.put("categoryRoots", categoryRoots);
        //查询子专业
        if (categoryId > 0) {
            List<Category> categoryChildren;
            //父Id大于0,获得它的同级专业
            if (categoryParentId > 0) {
                categoryChildren = categoryService.getChildren(categoryParentId);
                Category parent = categoryService.get(categoryParentId);
                model.put("categoryParent", parent);//父级 专业名
            } else {
                //获得它的子级专业
                categoryChildren = categoryService.getChildren(categoryId);
            }
            model.put("categoryChildren", categoryChildren);
        }
        CourseQuery query = new CourseQuery();
        query.setChannelId(channelId);
        query.setCategoryId(categoryId);
        query.setTeacherId(TKConvert.toInteger(request.getParameter("teacherId")));
        query.setMemberTypeId(TKConvert.toInteger(request.getParameter("memberTypeId")));
        query.setSellType(CourseType.getString(request.getParameter("sellType")));
        query.setIsFree(TKConvert.toInteger(request.getParameter("isFree")));
        //只查询上架的
        query.setIsAvailable(1);
        if ("true".equals(request.getParameter("sellTypeExclude"))) {
            query.setSellTypeExclude("LIVE");//如果是课程+套餐，直接排除直播
        }
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        if (searchKey.length() > 1) {
            query.setCourseName(TKString.searchKeyFilter(searchKey));
        }
        query.setOrderBy(TKConvert.toInteger(request.getParameter("orderBy")));
        // 页面传来的数据放到page中
        PageRequest page = new PageRequest();
        page.setCurrentPage(TKConvert.toInteger(request.getParameter("page")));
        page.setPageSize(12);
        int total = courseService.count(query);

        model.put("totalPages", page.getTotalPages(total));

        // 搜索课程列表
        List<Course> courseList = courseService.getList(query, page);
        model.put("courseList", courseList);

        Map<String, Object> serviceSwitch = websiteProfileService.getMap(WebsiteProfileType.serviceSwitch.toString());
        if ("ON".equals(serviceSwitch.get("member")) && ObjectUtils.isNotNull(memberTypeService)) {
            /*会员类型集合*/
            List<MemberType> memberTypeList = memberTypeService.getList(new MemberType(), null);
            model.put("memberTypeList", memberTypeList);
        }
        model.put("page", page);
        model.put("courseQuery", query);
        return getViewPath("distribution/distribution-list");
    }
}
